package com.kantboot.functional.third.party.google.service.impl;

import com.kantboot.functional.file.domain.entity.FunctionalFile;
import com.kantboot.functional.file.service.IFunctionalFileService;
import com.kantboot.functional.third.party.google.exception.ThirdPartyGoogleException;
import com.kantboot.functional.third.party.google.service.ITpGoogleService;
import com.kantboot.functional.third.party.google.setting.ThirdPartyGoogleSetting;
import com.kantboot.functional.third.party.google.util.web.login.Credential2Info;
import com.kantboot.system.user.domain.dto.ThirdPartyLoginDTO;
import com.kantboot.system.user.domain.entity.SysUser;
import com.kantboot.system.user.domain.entity.SysUserThirdParty;
import com.kantboot.system.user.domain.vo.LoginVO;
import com.kantboot.system.user.service.ISysThirdPartyService;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;

@Service
public class TPGoogleServiceImpl implements ITpGoogleService {

    @Resource
    private ThirdPartyGoogleSetting setting;

    @Resource
    private ISysThirdPartyService thirdPartyService;

    @Resource
    private IFunctionalFileService fileService;

    @Override
    public String getClientId() {
        return setting.getClientId();
    }

    @SneakyThrows
    @Override
    public LoginVO login(String credential) {
        try {
            Credential2Info.Result result = Credential2Info.getResult(
                    new Credential2Info.Param().setCredential(credential),
                    getClientId()
            );

            // 将谷歌用户信息转换为系统第三方用户信息
            ThirdPartyLoginDTO dto = new ThirdPartyLoginDTO();
            dto.setThirdPartyCode("google");
            dto.setKey("sub");
            dto.setValue(result.getSub());
            SysUserThirdParty sysUserThirdParty = thirdPartyService.get(dto);
            if (sysUserThirdParty != null) {
                return thirdPartyService.login(dto);
            }


            // 将谷歌用户信息中的picture转换为系统用户头像
            String avatar = result.getPicture();
            URL url = new URL(avatar);
            URLConnection connection = url.openConnection();
            InputStream inputStream = connection.getInputStream();
            // 假设你知道文件类型和文件名，你可能需要根据URL或连接来确定它们
            String contentType = connection.getContentType();
            String fileName = "avatar.png";
            // 将头像转换为MultipartFile
            MultipartFile avatarFile = new MockMultipartFile(fileName, fileName, contentType, inputStream);
            // 上传头像
            FunctionalFile functionalFile = fileService.upload(avatarFile, "avatar", null);

            // 将谷歌用户信息转换为系统用户信息
            SysUser user = new SysUser()
                    .setEmail(result.getEmail())
                    .setNickname(result.getName())
                    .setFileIdOfAvatar(functionalFile.getId());

            dto.setUser(user);


            return thirdPartyService.login(dto);
        }catch (SocketTimeoutException e) {
            // 如果谷歌连接超时，则抛出谷歌连接超时异常
            throw ThirdPartyGoogleException.GOOGLE_CONNECT_TIMEOUT;
        }catch (SocketException e){
            // 如果谷歌连接超时，则抛出谷歌连接超时异常
            throw ThirdPartyGoogleException.GOOGLE_CONNECT_TIMEOUT;
        }
    }

}
